package com.peng.dian.controller;

import com.peng.dian.service.searchAudioAndPicture;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;

@RestController
public class audioAndPictureController {

    @Autowired
    searchAudioAndPicture searchAudioAndPicture;
    @RequestMapping("/audioAndPicture")
    public HashMap<Object, Object> audioAndPicture(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HashMap<Object, Object> objectObjectHashMap = searchAudioAndPicture.searchEmployee();
        System.out.println(objectObjectHashMap.get("audio"));
        System.out.println(objectObjectHashMap.get("picture"));
        System.out.println(objectObjectHashMap);
        return objectObjectHashMap;
//        String filePath = "";
//        request.setCharacterEncoding("utf-8"); //设置编码
//        String realPath = request.getSession().getServletContext().getRealPath("/uploadFile/");
//        File dir = new File(realPath);
//        //文件目录不存在，就创建一个
//        if (!dir.isDirectory()) {
//            dir.mkdirs();
//        }
//        try {
//            StandardMultipartHttpServletRequest req = (StandardMultipartHttpServletRequest) request;      //获取formdata的值
//            Iterator<String> iterator = req.getFileNames();
//            String username = request.getParameter("username");
//            String password = request.getParameter("password");
//            String timedata = request.getParameter("timedata");
//            while (iterator.hasNext()) {
//                MultipartFile file = req.getFile(iterator.next());
//                String fileName = file.getOriginalFilename(); //真正写到磁盘上
//                String uuid = UUID.randomUUID().toString().replace("-", "");
//                String kzm = fileName.substring(fileName.lastIndexOf("."));
//                String filename = uuid + kzm;
//                File file1 = new File(realPath + filename);
//                OutputStream out = new FileOutputStream(file1);
//                out.write(file.getBytes());
//                out.close();
//                filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/uploadFile/" + filename;
//                System.out.println("访问图片路径:" + filePath + "====username:" + username);
//            }
//        } catch (Exception e) {
//            logger.error("", e);
//        }
//        return filePath;
    }
}
